#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>
int main()
{
    
    //2.1打卡文件，如果文件不存在则创建
    int fd=open("bite",O_RDWR|O_CREAT,0666);

    if(fd==-1)
    {
        perror("Failed to open file");
        return EXIT_FAILURE;
    }

    //向文件中写入字符串
    write(fd,"i hate linux!",13);

    //将文件指针重置到文件的开头
    lseek(fd,0,SEEK_SET);
    //2.2从文件中读取内容
    char buffer[100];
    int bytes_read =read(fd,buffer,strlen("i hate linux"));
    if(bytes_read==-1)
    {
        perror("Failed to read file");
        close(fd);
        return EXIT_FAILURE;
    }
    buffer[bytes_read]='\0';

    //打印
    printf("Fille content:%s\n",buffer);
    
    close(fd);
    return 0;
}
